package com.laiyw.monitor.core.system;

import com.alibaba.fastjson.JSONObject;
import com.laiyw.monitor.core.ServerMonitorInfo;
import com.laiyw.monitor.core.annotation.Service;
import com.laiyw.monitor.core.commons.OS;
import com.laiyw.monitor.utils.AddressUtils;
import com.laiyw.monitor.utils.ByteUtils;
import org.hyperic.sigar.*;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Auther: Laiyw
 * @Date: 2018/10/4 17:32
 */
@Service(OS.WINDOWS)
public class WinServerMonitorInfo extends ServerMonitorInfo {

    @Override
    public Double cpuUsedPercent() {
        //return CpuUtils.getCpuRatioForWindows();
        try {
            Sigar sigar = new Sigar();
            CpuInfo infos[] = sigar.getCpuInfoList();
            CpuPerc precList[] = sigar.getCpuPercList();
            Double allCom = 0D;
            for (int i = 0; i < infos.length; i++) {
                allCom += precList[i].getCombined();
            }
            return allCom / infos.length * 100;
        } catch (Exception e) {
            e.printStackTrace();
            return new Double(0);
        }
    }

    @Override
    public Double sysMemoryUsedPercent() {
        try {
            Mem mem = new Sigar().getMem();
            return mem.getUsed() * 1.0 / mem.getTotal() * 100;
        } catch (SigarException e) {
            e.printStackTrace();
            return new Double(0);
        }
    }

    @Override
    public JSONObject getServerInfo() {
        JSONObject json = new JSONObject();
        OperatingSystemMXBean osb = ManagementFactory.getOperatingSystemMXBean();
        json.put("name", osb.getName());//操作系统
        json.put("arch", osb.getArch());//架构
        json.put("version", osb.getVersion());//版本
        json.put("availableProcessors", osb.getAvailableProcessors());//可用的处理器个数
        json.put("systemLoadAverage", osb.getSystemLoadAverage());//系统负载平均值
        try {
            json.put("address", AddressUtils.getServerIp());//本机ip
            json.put("hostName", InetAddress.getLocalHost().getHostName());//本机计算机名称
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public List<JSONObject> getServerDisk() {
        List<JSONObject> jsonObjectList = new ArrayList<JSONObject>();
        try {
            JSONObject json = null;
            Sigar sigar = new Sigar();
            FileSystem fslist[] = sigar.getFileSystemList();
            for (FileSystem fs : fslist) {
                json = new JSONObject();
                // 分区的盘符名称
                json.put("devName", fs.getDevName());
                // 分区的盘符路径
                json.put("dirName", fs.getDirName());
                // 文件系统类型，比如 FAT32、NTFS
                json.put("sysTypeName", fs.getSysTypeName());
                // 文件系统类型名，比如本地硬盘、光驱、网络文件系统等
                json.put("typeName", fs.getTypeName());
                FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
                switch (fs.getType()) {
                    case 0: // TYPE_UNKNOWN ：未知
                        break;
                    case 1: // TYPE_NONE
                        break;
                    case 2: // TYPE_LOCAL_DISK : 本地硬盘
                        // 文件系统总大小
                        json.put("total", ByteUtils.formatByteSize(usage.getTotal()));
                        // 文件系统剩余大小
                        json.put("free", ByteUtils.formatByteSize(usage.getFree()));
                        // 文件系统可用大小
                        json.put("avail", ByteUtils.formatByteSize(usage.getAvail()));
                        // 文件系统已经使用量
                        json.put("used", ByteUtils.formatByteSize(usage.getUsed()));
                        // 文件系统资源的利用率
                        json.put("usedPercent", (usage.getUsePercent() * 100D) + "%");
                        break;
                    case 3:// TYPE_NETWORK ：网络
                        break;
                    case 4:// TYPE_RAM_DISK ：闪存
                        break;
                    case 5:// TYPE_CDROM ：光驱
                        break;
                    case 6:// TYPE_SWAP ：页面交换
                        break;
                }
                json.put("read", ByteUtils.formatByteSize(usage.getDiskReads()));
                json.put("write", ByteUtils.formatByteSize(usage.getDiskWrites()));
                jsonObjectList.add(json);
            }
            return jsonObjectList;
        } catch (Exception e) {
            e.printStackTrace();
            return jsonObjectList;
        }
    }

    @Override
    public List<JSONObject> getServerCpuListInfo() {
        try {
            Sigar sigar = new Sigar();
            CpuInfo infos[] = sigar.getCpuInfoList();
            CpuPerc precList[] = sigar.getCpuPercList();
            List<JSONObject> jsonObjectList = new ArrayList<>();
            JSONObject json = null;
            for (int i = 0; i < infos.length; i++) {
                json = new JSONObject();
                CpuInfo info = infos[i];
                json.put("mhz", info.getMhz());
                json.put("vendor", info.getVendor());//生产商
                json.put("model", info.getModel());//类别
                json.put("cacheSize", info.getCacheSize());//缓存数量
                json.put("precUser", CpuPerc.format(precList[i].getUser()));// 用户使用率
                json.put("precSys", CpuPerc.format(precList[i].getSys()));// 系统使用率
                json.put("precWait", CpuPerc.format(precList[i].getWait()));// 当前等待率
                json.put("precNice", CpuPerc.format(precList[i].getNice()));//当前错误率
                json.put("precIdle", CpuPerc.format(precList[i].getIdle()));// 当前空闲率
                json.put("precCombined", CpuPerc.format(precList[i].getCombined()));// 总的使用率
                jsonObjectList.add(json);
            }
            return jsonObjectList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JSONObject getServerMemory() {
        JSONObject json = new JSONObject();
        try {
            Sigar sigar = new Sigar();
            Mem mem = sigar.getMem();
            json.put("memTotal", ByteUtils.formatByteSize(mem.getTotal()));
            json.put("memUsed", ByteUtils.formatByteSize(mem.getUsed()));
            json.put("memFree", ByteUtils.formatByteSize(mem.getFree()));
            json.put("memUsedPercent", new DecimalFormat("#0.00%").format(mem.getUsed() * 1.0 / mem.getTotal()));
            // 交换区
            Swap swap = sigar.getSwap();
            json.put("swapTotal", ByteUtils.formatByteSize(swap.getTotal()));
            json.put("swapUsed", ByteUtils.formatByteSize(swap.getUsed()));
            json.put("swapFree", ByteUtils.formatByteSize(swap.getFree()));
            json.put("swapUsedPercent", new DecimalFormat("#0.00%").format(swap.getUsed() * 1.0 / swap.getTotal()));
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return json;
        }
    }
}
